package net.mrsigma.tetris.controller;

import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

import net.mrsigma.tetris.entitles.Ground;
import net.mrsigma.tetris.entitles.Shape;
import net.mrsigma.tetris.entitles.ShapeFactory;
import net.mrsigma.tetris.listener.ShapeListener;
import net.mrsigma.tetris.view.GamePanel;

public class Controller extends KeyAdapter implements ShapeListener{

	private Shape shape;
	private ShapeFactory shapeFactory;
	private Ground ground;
	private GamePanel gamePanel;
	
	
	@Override
	public void keyPressed(KeyEvent e) {
		// TODO Auto-generated method stub
		switch(e.getKeyCode()) {
		case KeyEvent.VK_UP:
			shape.rotate();
			break;
		case KeyEvent.VK_DOWN:
			shape.moveDown();
			break;
		case KeyEvent.VK_LEFT:
			shape.moveLeft();
			break;
		case KeyEvent.VK_RIGHT:
			shape.moveRight();
			break;
		}
		gamePanel.display(ground, shape);
	}


	@Override
	public void shapeMoveDown(Shape shape) {
		// TODO Auto-generated method stub
		gamePanel.display(ground, shape);
	}

	public void newGame() {
		shape = shapeFactory.getShape(this);
	}
	
	public Controller(ShapeFactory shapeFactory, 
			Ground ground, GamePanel gamePanel) {
		this.shapeFactory = shapeFactory;
		this.ground = ground;
		this.gamePanel = gamePanel;
	}
}
